/**
 * 
 */
package gpl.scotlandyard.android.activities;

import gpl.scotlandyard.android.R;
import gpl.scotlandyard.android.controllers.BasicController;
import gpl.scotlandyard.android.controllers.IController;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

/** @author Norbert */
public abstract class AbstractActivity extends Activity {

  private IController controller = new BasicController();

  /**
   * 
   */
  public AbstractActivity() {
    controller = new BasicController();
    controller.setActivity(this);
  }

  /**
   * Sets the controller the activity will interact with.
   * 
   * @param controller
   */
  public final void setController(IController controller) {
    this.controller = controller;
  }

  /** @return controller */
  public IController getController() {
    return controller;
  }

  @Override
  public final void onBackPressed() {
    getController().onBackPressed();
  }

  @Override
  public final boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_SEARCH) {
      Toast.makeText(this, R.string.msg_search_hard_button, Toast.LENGTH_SHORT).show();
    }
    return super.onKeyDown(keyCode, event);
  }

  @Override
  public final boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);
    onCreateOptionsMenuChild(menu);
    return true;
  }

  /**
   * Called on {@link #onCreateOptionsMenu(Menu)}.
   * 
   * @param menu
   */
  protected abstract void onCreateOptionsMenuChild(Menu menu);

  /**
   * Called on {@link #onOptionsItemSelected(MenuItem)}.
   * 
   * @param item
   * @return
   */
  protected abstract boolean onOptionsItemSelectedChild(MenuItem item);

  @Override
  public final boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.option_m_mail) {
      Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
      emailIntent.setType("plain/text");
      String[] dest = { "norbertmartin12@gmail.com" };
      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, dest);
      try {
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getPackageManager().getPackageInfo(getPackageName(), 0).versionName);
      } catch (NameNotFoundException e) {
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getText(R.string.unknown_version));
      }
      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, getText(R.string.option_m_mail_content));
      startActivity(emailIntent);
      return true;
    }
    if (onOptionsItemSelectedChild(item)) {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }

}
